﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace Maticsoft.DAL
{
	/// <summary>
	/// 数据访问类:ReportSubmit
	/// </summary>
	public partial class ReportSubmit
	{
		public ReportSubmit()
		{}
		#region  Method


		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(string RS_Id,string T_Id,string S_Id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from ReportSubmit");
			strSql.Append(" where RS_Id='"+RS_Id+"' and T_Id='"+T_Id+"' and S_Id='"+S_Id+"' ");
			return DbHelperSQL.Exists(strSql.ToString());
		}

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(Maticsoft.Model.ReportSubmit model)
		{
			StringBuilder strSql=new StringBuilder();
			StringBuilder strSql1=new StringBuilder();
			StringBuilder strSql2=new StringBuilder();
			if (model.RS_Id != null)
			{
				strSql1.Append("RS_Id,");
				strSql2.Append("'"+model.RS_Id+"',");
			}
			if (model.T_Id != null)
			{
				strSql1.Append("T_Id,");
				strSql2.Append("'"+model.T_Id+"',");
			}
			if (model.S_Id != null)
			{
				strSql1.Append("S_Id,");
				strSql2.Append("'"+model.S_Id+"',");
			}
			if (model.RS_Title != null)
			{
				strSql1.Append("RS_Title,");
				strSql2.Append("'"+model.RS_Title+"',");
			}
			if (model.RS_Formerly != null)
			{
				strSql1.Append("RS_Formerly,");
				strSql2.Append("'"+model.RS_Formerly+"',");
			}
			if (model.RS_Path != null)
			{
				strSql1.Append("RS_Path,");
				strSql2.Append("'"+model.RS_Path+"',");
			}
			if (model.RS_Correcting != null)
			{
				strSql1.Append("RS_Correcting,");
				strSql2.Append("'"+model.RS_Correcting+"',");
			}
			if (model.RS_Socre != null)
			{
				strSql1.Append("RS_Socre,");
				strSql2.Append("'"+model.RS_Socre+"',");
			}
			if (model.RS_Ask != null)
			{
				strSql1.Append("RS_Ask,");
				strSql2.Append("'"+model.RS_Ask+"',");
			}
			if (model.RS_Ans != null)
			{
				strSql1.Append("RS_Ans,");
				strSql2.Append("'"+model.RS_Ans+"',");
			}
			if (model.RS_Share != null)
			{
				strSql1.Append("RS_Share,");
				strSql2.Append("'"+model.RS_Share+"',");
			}
			if (model.RS_Date != null)
			{
				strSql1.Append("RS_Date,");
				strSql2.Append("'"+model.RS_Date+"',");
			}
			strSql.Append("insert into ReportSubmit(");
			strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
			strSql.Append(")");
			strSql.Append(" values (");
			strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
			strSql.Append(")");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Maticsoft.Model.ReportSubmit model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update ReportSubmit set ");
			if (model.RS_Title != null)
			{
				strSql.Append("RS_Title='"+model.RS_Title+"',");
			}
			if (model.RS_Formerly != null)
			{
				strSql.Append("RS_Formerly='"+model.RS_Formerly+"',");
			}
			else
			{
				strSql.Append("RS_Formerly= null ,");
			}
			if (model.RS_Path != null)
			{
				strSql.Append("RS_Path='"+model.RS_Path+"',");
			}
			else
			{
				strSql.Append("RS_Path= null ,");
			}
			if (model.RS_Correcting != null)
			{
				strSql.Append("RS_Correcting='"+model.RS_Correcting+"',");
			}
			else
			{
				strSql.Append("RS_Correcting= null ,");
			}
			if (model.RS_Socre != null)
			{
				strSql.Append("RS_Socre='"+model.RS_Socre+"',");
			}
			else
			{
				strSql.Append("RS_Socre= null ,");
			}
			if (model.RS_Ask != null)
			{
				strSql.Append("RS_Ask='"+model.RS_Ask+"',");
			}
			else
			{
				strSql.Append("RS_Ask= null ,");
			}
			if (model.RS_Ans != null)
			{
				strSql.Append("RS_Ans='"+model.RS_Ans+"',");
			}
			else
			{
				strSql.Append("RS_Ans= null ,");
			}
			if (model.RS_Share != null)
			{
				strSql.Append("RS_Share='"+model.RS_Share+"',");
			}
			else
			{
				strSql.Append("RS_Share= null ,");
			}
			if (model.RS_Date != null)
			{
				strSql.Append("RS_Date='"+model.RS_Date+"',");
			}
			else
			{
				strSql.Append("RS_Date= null ,");
			}
			int n = strSql.ToString().LastIndexOf(",");
			strSql.Remove(n, 1);
			strSql.Append(" where RS_Id='"+ model.RS_Id+"' and T_Id='"+ model.T_Id+"' and S_Id='"+ model.S_Id+"' ");
			int rowsAffected=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(string RS_Id,string T_Id,string S_Id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from ReportSubmit ");
			strSql.Append(" where RS_Id='"+RS_Id+"' and T_Id='"+T_Id+"' and S_Id='"+S_Id+"' " );
			int rowsAffected=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Maticsoft.Model.ReportSubmit GetModel(string RS_Id,string T_Id,string S_Id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1  ");
			strSql.Append(" RS_Id,T_Id,S_Id,RS_Title,RS_Formerly,RS_Path,RS_Correcting,RS_Socre,RS_Ask,RS_Ans,RS_Share,RS_Date ");
			strSql.Append(" from ReportSubmit ");
			strSql.Append(" where RS_Id='"+RS_Id+"' and T_Id='"+T_Id+"' and S_Id='"+S_Id+"' " );
			Maticsoft.Model.ReportSubmit model=new Maticsoft.Model.ReportSubmit();
			DataSet ds=DbHelperSQL.Query(strSql.ToString());
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Maticsoft.Model.ReportSubmit DataRowToModel(DataRow row)
		{
			Maticsoft.Model.ReportSubmit model=new Maticsoft.Model.ReportSubmit();
			if (row != null)
			{
				if(row["RS_Id"]!=null)
				{
					model.RS_Id=row["RS_Id"].ToString();
				}
				if(row["T_Id"]!=null)
				{
					model.T_Id=row["T_Id"].ToString();
				}
				if(row["S_Id"]!=null)
				{
					model.S_Id=row["S_Id"].ToString();
				}
				if(row["RS_Title"]!=null)
				{
					model.RS_Title=row["RS_Title"].ToString();
				}
				if(row["RS_Formerly"]!=null)
				{
					model.RS_Formerly=row["RS_Formerly"].ToString();
				}
				if(row["RS_Path"]!=null)
				{
					model.RS_Path=row["RS_Path"].ToString();
				}
				if(row["RS_Correcting"]!=null)
				{
					model.RS_Correcting=row["RS_Correcting"].ToString();
				}
				if(row["RS_Socre"]!=null)
				{
					model.RS_Socre=row["RS_Socre"].ToString();
				}
				if(row["RS_Ask"]!=null)
				{
					model.RS_Ask=row["RS_Ask"].ToString();
				}
				if(row["RS_Ans"]!=null)
				{
					model.RS_Ans=row["RS_Ans"].ToString();
				}
				if(row["RS_Share"]!=null)
				{
					model.RS_Share=row["RS_Share"].ToString();
				}
				if(row["RS_Date"]!=null)
				{
					model.RS_Date=row["RS_Date"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
        /// 

        public DataSet GetShareInformation(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Student.S_Id,S_Name,Teacher.T_Id,Teacher.T_Name,RS_Id,RS_Title,RS_Formerly,RS_Path,RS_Correcting,RS_Socre,RS_Ask,RS_Ans,RS_Share,RS_Date ");
            strSql.Append(" FROM ReportSubmit,Student,Teacher ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }


        public DataSet GetAllInformation()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            strSql.Append(" RS_Id,T_Id,S_Id,RS_Title,RS_Formerly,RS_Path,RS_Correcting,RS_Socre,RS_Ask,RS_Ans,RS_Share,RS_Date ");
            strSql.Append(" FROM ReportSubmit ");

            return DbHelperSQL.Query(strSql.ToString());
        }


		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select Teacher.T_Id,T_Name,RS_Id,RS_Title,RS_Formerly,RS_Path,RS_Correcting,RS_Socre,RS_Ask,RS_Ans,RS_Share,RS_Date ");
            strSql.Append(" FROM ReportSubmit,Teacher");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" RS_Id,T_Id,S_Id,RS_Title,RS_Formerly,RS_Path,RS_Correcting,RS_Socre,RS_Ask,RS_Ans,RS_Share,RS_Date ");
			strSql.Append(" FROM ReportSubmit ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM ReportSubmit ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.S_Id desc");
			}
			strSql.Append(")AS Row, T.*  from ReportSubmit T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		*/

		#endregion  Method
		#region  MethodEx

		#endregion  MethodEx
	}
}

